//
//  PasswordLoginViewController.swift
//  clreader
//
//  Created by colin on 2019/3/26.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

/// 账号密码登录
class PasswordLoginViewController: CLBaseViewController {

    private lazy var textFieldPhone = UITextField(frame: CGRect.zero)
    private lazy var textFieldPassword = UITextField(frame: CGRect.zero)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        let buttonBack = UIButton(type: .custom).then {
            $0.setImage(UIImage(named: "button_back"), for: .normal)
            $0.addTarget(self, action: #selector(self.actionBack(_:)), for: .touchUpInside)
        }
        self.view.addSubview(buttonBack)
        buttonBack.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
            make.size.equalTo(CGSize(width: 44.0, height: 44.0))
        }
        
        // 手机号
        let labelPhone = UILabel().then {
            $0.text = "输入手机号"
            $0.textColor = UIColor(hex: "343434")
            $0.font = UIFont.systemFont(ofSize: 18.0, weight: .light)
        }
        self.view.addSubview(labelPhone)
        labelPhone.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(60.0)
            make.top.equalTo(buttonBack.snp.bottom).offset(40.0)
        }
        self.textFieldPhone.do {
            $0.textColor = UIColor(hex: "343434")
            $0.font = UIFont.systemFont(ofSize: 22.0, weight: .light)
            $0.placeholder = "请输入您的手机号"
            $0.keyboardType = .numberPad
            $0.clearButtonMode = .always
        }
        self.view.addSubview(self.textFieldPhone)
        self.textFieldPhone.snp.makeConstraints { (make) in
            make.left.equalTo(labelPhone.snp.left)
            make.top.equalTo(labelPhone.snp.bottom).offset(20.0)
            make.right.equalToSuperview().offset(-60.0)
            make.height.equalTo(44.0)
        }
        let linePhone = UIView()
        linePhone.backgroundColor = UIColor(hex: "F0F0F0")
        self.view.addSubview(linePhone)
        linePhone.snp.makeConstraints { (make) in
            make.left.equalTo(self.textFieldPhone.snp.left)
            make.right.equalTo(self.textFieldPhone.snp.right)
            make.top.equalTo(self.textFieldPhone.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        // 密码
        let labelPassword = UILabel().then {
            $0.text = "输入密码"
            $0.textColor = UIColor(hex: "343434")
            $0.font = UIFont.systemFont(ofSize: 18.0, weight: .light)
        }
        self.view.addSubview(labelPassword)
        labelPassword.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(60.0)
            make.top.equalTo(linePhone.snp.bottom).offset(40.0)
        }
        self.textFieldPassword.do {
            $0.textColor = UIColor(hex: "343434")
            $0.font = UIFont.systemFont(ofSize: 22.0, weight: .light)
            $0.placeholder = "请输入密码"
            $0.clearButtonMode = .always
            $0.isSecureTextEntry = true
        }
        self.view.addSubview(self.textFieldPassword)
        self.textFieldPassword.snp.makeConstraints { (make) in
            make.left.equalTo(labelPassword.snp.left)
            make.top.equalTo(labelPassword.snp.bottom).offset(20.0)
            make.right.equalToSuperview().offset(-60.0)
            make.height.equalTo(44.0)
        }
        let linePassword = UIView()
        linePassword.backgroundColor = UIColor(hex: "F0F0F0")
        self.view.addSubview(linePassword)
        linePassword.snp.makeConstraints { (make) in
            make.left.equalTo(self.textFieldPassword.snp.left)
            make.right.equalTo(self.textFieldPassword.snp.right)
            make.top.equalTo(self.textFieldPassword.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        
        // 登录按钮
        let buttonLogin = UIButton(type: .custom).then {
            $0.backgroundColor = UIColor(hex: "AE1600")
            $0.layer.cornerRadius = 24.5
            $0.layer.masksToBounds = true
            $0.setTitle("登录", for: .normal)
            $0.setTitleColor(UIColor.white, for: .normal)
            $0.titleLabel?.font = UIFont.systemFont(ofSize: 17.0, weight:.bold)
            $0.addTarget(self, action: #selector(self.actionLogin(_:)), for: .touchUpInside)
        }
        self.view.addSubview(buttonLogin)
        buttonLogin.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(50.0)
            make.right.equalToSuperview().offset(-50.0)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-50.0)
            make.height.equalTo(49.0)
        }
        
    }

}

extension PasswordLoginViewController {
    @objc private func actionBack(_ sender:Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc private func actionLogin(_ sender:Any) {
        guard let phone = self.textFieldPhone.text?.trimmingCharacters(in: .whitespacesAndNewlines), !phone.isEmpty else {
            self.hud.showText("请输入手机号")
            return
        }
        guard let password = self.textFieldPassword.text?.trimmingCharacters(in: .whitespacesAndNewlines), !password.isEmpty else {
            self.hud.showText("请输入密码")
            return
        }
        
        self.hud.showProgress()
        _ = Router.login(phone: phone, member_pwd: password).request { [weak self] (response:RequestResponse<JSONObject>) in
            
            self?.hud.hide()
            
            if case .success(let res,let message) = response {
                self?.hud.showText(message)
                // 登录成功
                if let token = res?["token"] as? String {
                    AccountManager.shared.token = token
                }
                AccountManager.shared.isLogin = true
                
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                    self?.navigationController?.dismiss(animated: true, completion: nil)
                })
                
            }
            
            if case .failure(_, let message) = response {
                self?.hud.showText(message)
            }
        }
        
    }
}
